package org.softmed.rest.admin.resource;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.softmed.rest.admin.interfaces.RemoveListener;
import org.softmed.rest.admin.module.ValidationException;
import org.softmed.swing.IconManager;

import restlet.description.convert.RESTResourceDescription;


public class ResourceHeader extends JPanel implements ActionListener,
		KeyListener, DocumentListener {

	JTextField resource = new JTextField();
	JButton remove = new JButton(IconManager.remove);

	RESTResourceDescription resourceDescription;

	RemoveListener removeListener;

	public ResourceHeader() {
		resource.addKeyListener(this);
		resource.getDocument().addDocumentListener(this);
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		setPreferredSize(new Dimension(540, 25));
		setMaximumSize(new Dimension(540, 25));
		setMinimumSize(new Dimension(540, 25));

		JLabel label = new JLabel("Resource URI");
		label.setPreferredSize(new Dimension(80, 25));
		label.setMaximumSize(new Dimension(80, 25));
		label.setMinimumSize(new Dimension(80, 25));

		resource.setPreferredSize(new Dimension(440, 25));
		resource.setMaximumSize(new Dimension(440, 25));
		resource.setMinimumSize(new Dimension(440, 25));

		remove.setPreferredSize(new Dimension(20, 20));
		remove.setMaximumSize(new Dimension(20, 20));
		remove.setMinimumSize(new Dimension(20, 20));

		remove.addActionListener(this);

		add(label);
		add(resource);
		add(remove);

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		removeListener.remove(this);

	}

	public RemoveListener getRemoveListener() {
		return removeListener;
	}

	public void setRemoveListener(RemoveListener removeListener) {
		this.removeListener = removeListener;
	}

	public void validateData() throws ValidationException {
		if (resource.getText() == null || resource.getText().length() == 0) {
			throw new ValidationException("Resource URL can't be empty !!! ");
		}

	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		Object source = arg0.getSource();
		if (source == resource) {
			resourceDescription.setUri(resource.getText());
			return;
		}

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public RESTResourceDescription getResourceDescription() {
		return resourceDescription;
	}

	public void setResourceDescription(
			RESTResourceDescription resourceDescription) {
		this.resourceDescription = resourceDescription;
		resource.setText(resourceDescription.getUri());
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		resourceDescription.setUri(resource.getText());
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		resourceDescription.setUri(resource.getText());
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		// TODO signal change ?
		resourceDescription.setUri(resource.getText());
		
	}

}
